package zzs.generalframework.console.core.user.usereducation.dao.impl;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import zzs.generalframework.common.core.user.usereducation.UserEducationInfo;
import zzs.generalframework.common.core.user.usereducation.UserEducationPO;
import zzs.generalframework.console.core.user.usereducation.dao.IUserEducationDao;
import zzs.generalframework.console.util.QueryParameter;
import zzs.generalframework.util.core.string.StringUtils;

/**
 * @title
 * @description
 * @project zzs-console
 * @package zzs.generalframework.console.core.user.usereducation.dao.impl
 * @author zzs
 * @date 2018年2月5日
 * @version 1.0
 */
@Repository("zzs.generalframework.console.core.user.usereducation.dao.impl.userEducationDaoImpl")
public class UserEducationDaoImpl implements IUserEducationDao
{
	private Logger logger = LoggerFactory.getLogger(UserEducationDaoImpl.class);
	@PersistenceContext(unitName = "zzs-entity")
	EntityManager entityManager;
	@Autowired
	JdbcTemplate jdbcTemplate;

	@Override
	public boolean insert(UserEducationPO userEducationPO)
	{
		logger.info("====插入用户教育数据开始====");
		logger.debug("====插入用户教育数据,入参:UserEducationPO=[{}]====", userEducationPO);
		entityManager.persist(userEducationPO);
		logger.debug("====插入用户教育数据,出参:boolean=[{}]====", true);
		logger.info("====插入用户教育数据结束====");
		return true;
	}

	@Override
	public boolean update(UserEducationPO userEducationPO)
	{
		logger.info("====更新用户教育数据开始====");
		logger.debug("====更新用户教育数据,入参:UserEducationPO=[{}]====", userEducationPO);
		entityManager.merge(userEducationPO);
		logger.debug("====更新数据,出参:boolean=[{}]====", true);
		logger.info("====更新用户教育数据结束====");
		return true;
	}

	@Override
	public List<UserEducationPO> select(UserEducationInfo userEducationInfo)
	{
		logger.info("====查询用户教育数据开始====");
		logger.debug("====查询用户教育数据,入参:UserEducationInfo=[{}]====", userEducationInfo);
		List<UserEducationPO> returnList = null;
		StringBuffer sqlBuffer = new StringBuffer();
		QueryParameter parameter = new QueryParameter();
		sqlBuffer.append("select a from UserEducationPO a where 1=1 ");
		if (StringUtils.isNotNullAndBlank(userEducationInfo.getUserEducationID()))
		{
			sqlBuffer.append(" and a.userEducationID=?");
			parameter.putParameterObject(userEducationInfo.getUserEducationID());
		}
		logger.info("查询数据SQL:[{}]", sqlBuffer.toString());
		Query query = entityManager.createQuery(sqlBuffer.toString());
		parameter.setQueryParameter(query);
		returnList = query.getResultList();
		logger.debug("====查询用户教育数据,出参:List<UserEducationPO>=[{}]====", returnList);
		logger.info("====查询用户教育数据结束====");
		return returnList;
	}

	@Override
	public boolean delete(UserEducationPO userEducationPO)
	{
		logger.info("====删除用户教育数据开始====");
		logger.debug("====删除用户教育数据,入参:UserEducationPO=[{}]====", userEducationPO);
		entityManager.remove(userEducationPO);
		logger.debug("====删除用户教育数据,出参:boolean=[{}]====", true);
		logger.info("====删除用户教育数据结束====");
		return true;
	}

	@Override
	public Long count(UserEducationPO userEducationPO)
	{
		logger.info("====统计用户教育数据开始====");
		logger.debug("====统计用户教育数据,入参:UserEducationPO=[{}]====", userEducationPO);
		Long returnLong = null;
		StringBuffer sqlBuffer = new StringBuffer();
		QueryParameter parameter = new QueryParameter();
		sqlBuffer.append("select count(*) from UserEducationPO a where 1=1 ");
		if (StringUtils.isNotNullAndBlank(userEducationPO.getUserEducationID()))
		{
			sqlBuffer.append(" and a.userEducationID=?");
			parameter.putParameterObject(userEducationPO.getUserEducationID());
		}
		logger.info("统计数据SQL:[{}]", sqlBuffer.toString());
		Query query = entityManager.createQuery(sqlBuffer.toString());
		parameter.setQueryParameter(query);
		returnLong = (Long) query.getSingleResult();
		logger.debug("====统计用户教育数据,出参:Long=[{}]====", returnLong);
		logger.info("====统计用户教育数据结束====");
		return returnLong;
	}

}
